package com.wmb.userservice.mapper;


import com.wmb.userservice.model.pojo.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {


    /**
     * 根据用户名查询用户
     * @param username
     * @return
     */
    @Select("select * from users where username = #{username}")
    User getUserByUsername(String username);
    /**
     * 根据邮箱查询用户
     * @param email
     * @return
     */
    @Select("select count(*) from users where email = #{email}")
    Integer getUserByEmail(String email);
    /**
     * 根据手机号查询用户
     * @param phone
     * @return
     */
    @Select("select count(*) from users where phone = #{phone}")
    Integer getUserByPhone(String phone);


    /**
     * 新增用户
     * @param user
     * @return
     */
    @Insert("insert into users (username,password,email,phone,gmt_create) " +
            "values (#{username},#{password},#{email},#{phone},#{gmtCreate})" )
    @Options(useGeneratedKeys = true, keyColumn = "user_id", keyProperty = "userId")
    Integer insert(User user);


    /**
     * 根据用户id查询用户
     * @param userId
     * @return
     */
    @Select("select * from users where user_id = #{userId}")
    User getUserByUserId(Long userId);

    @Select("select role_id from user_roles where user_id = #{userId}")
    Integer getRoleId(Long userId);

    @Select("select * from users limit #{start},#{pageSize}")
    List<User> getUsersList(int start, int pageSize);

    @Select("SELECT u.* FROM users u INNER JOIN user_roles ur ON u.user_id = ur.user_id WHERE ur.role_id IN (2, 3) limit #{start},#{pageSize}")
    List<User> getUsersListByInnerJoin(int start, int pageSize);

    void updateUser(User user);

    /**
     * 重置密码
     *
     * @param userId
     * @param newPassword
     */
    @Update("UPDATE users SET password = #{newPassword} WHERE user_id = #{userId}")
    void resetPassword(Long userId, String newPassword);
}
